Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

195
Views
use my 1 round code to make a 3 rounds "Rock Paper Scissors" game - Javascript

I'm building a rock paper scissors game with Javascript, i did a 1 round mode of the game and it work, now i want to add a second mode " best of three rounds" but after trying many things i got my code messy and can't figure out what to do exactly.

i try to add a count = 0; count < 3; count = count + 1but don't know exactly where to put it

can someone help me please?

var mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
if (mode === '1') {
  oneRound;
}
if (mode === '2') {
  bestOfThree;
}

// set Computer Choice 

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
  computerChoice = "rock";
} else if (computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
}

// Compare value for oneRound mode


var oneRound = function(computerChoice, userChoice) {
  if (computerChoice === userChoice) {
    return "The result is tie!";
  }
  if (computerChoice === "rock") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "paper")
        return "Player wins";
    }
  }
  if (computerChoice === "paper") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "rock")
        return "Player wins";
    }
  }
  if (computerChoice === "scissors") {
    if (userChoice === "rock") {
      return "Computer wins";
    } else {
      if (userChoice === "scissors")
        return "Player wins";
    }
  }

};

console.log("Player Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
console.log(oneRound(computerChoice, userChoice));

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Have look at this

I use setTimeout to allow the screen to show the result

const play = () => {
  // set Computer Choice 
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if (computerChoice <= 0.67) {
    computerChoice = "paper";
  } else {
    computerChoice = "scissors";
  }

  console.log("Player Choice: " + userChoice);
  console.log("Computer Choice: " + computerChoice);


  if (computerChoice === userChoice) {
    return "The result is tie!";
  }
  if (computerChoice === "rock") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "paper")
        return "Player wins";
    }
  }
  if (computerChoice === "paper") {
    if (userChoice === "scissors") {
      return "Computer wins";
    } else {
      if (userChoice === "rock")
        return "Player wins";
    }
  }
  if (computerChoice === "scissors") {
    if (userChoice === "rock") {
      return "Computer wins";
    } else {
      if (userChoice === "scissors")
        return "Player wins";
    }
  }
};
const round = () => {
  const res = play();
  console.log(res)
  cnt--
  wins[cnt] = res.startsWith("Player") ? 1 : 0;
  if (cnt === 0) {
    const total = wins.reduce((a, b) => a + b)
    console.log(`You beat the computer ${total} time${total===1?"":"s"}`)
    return
  }
  setTimeout(round, 10) // else go again
}

let cnt = 1,
  wins = [];
const mode = prompt("Please press 1 for single game mode or 2 for best out of 3 mode");
if (mode === '2') {
  cnt = 3;
}
round()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!